package com.spring.boot.rabbit.service;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.spring.boot.rabbit.constant.ExchangeConst.DIRECT_EXCHANGE;
import static com.spring.boot.rabbit.constant.ExchangeConst.TOPIC_EXCHANGE;
import static com.spring.boot.rabbit.constant.RoutingKey.TRADING;
import static com.spring.boot.rabbit.constant.RoutingKey.TRADING_ACCOUNT;

/**
 * RabbitMQ生产者
 *
 * @author 李坤
 * @date 2022/9/1 11:47
 */
@Service
public class RabbitProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMessage2Direct(Object message) {
        String value = (message instanceof String) ? (String) message : JSON.toJSONString(message);
        rabbitTemplate.convertAndSend(DIRECT_EXCHANGE, TRADING, value, new CorrelationData(IdUtil.fastUUID()));
    }

    public void sendMessage2Topic(Object message) {
        String value = (message instanceof String) ? (String) message : JSON.toJSONString(message);
        rabbitTemplate.convertAndSend(TOPIC_EXCHANGE, TRADING_ACCOUNT, value, new CorrelationData(IdUtil.fastUUID()));
    }

}
